//TODO: 1.Build in Option to delete and replace Elements, 2.Build in Option to build Elements vertical 3.Build in PowerUp's

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class LevelEditor : EditorWindow
{
    private static List<GameObject> prefabs;
    //private float height;
    private float scale1, scale2, scale3;
    private float xcounter;

    [MenuItem("Level Editor/New Level")]
    public static void ShowWindow()
    {
        GetWindow(typeof(LevelEditor));
        prefabs = new List<GameObject>();
    }

    private void OnGUI()
    {
        GUILayout.Label("xcounter");
        GUILayout.TextField(this.xcounter.ToString());
        GUILayout.Label("x");
        GUILayout.TextField(this.scale1.ToString());
        GUILayout.Label("y");
        GUILayout.TextField(this.scale2.ToString());
        GUILayout.Label("z");
        GUILayout.TextField(this.scale3.ToString());
    }

    private void Start()
    {
    }

    private void Update()
    {
        const float height = 0;
        float xcounter = 0;

        //Adjust Position for GameObjects in the "prefabs" Array
        for (int i = 0; i < prefabs.Count; i++)
        {
            //Check if Object exists
            if (prefabs[i] != null && prefabs[i].transform != null)
            {
                prefabs[i].transform.position = new Vector3(xcounter, height, -2);

                this.scale1 = (prefabs[i].transform.renderer.bounds.max.x - prefabs[i].transform.renderer.bounds.min.x);
                this.scale2 = (prefabs[i].transform.renderer.bounds.max.y - prefabs[i].transform.renderer.bounds.min.y);
                this.scale3 = (prefabs[i].transform.renderer.bounds.max.z - prefabs[i].transform.renderer.bounds.min.z);

                //Try to add wider Elements
                xcounter += (prefabs[i].transform.renderer.bounds.max.x - prefabs[i].transform.renderer.bounds.min.x);
            }

            if (prefabs[i] == null)
            {
                this.scale1 = 0;
                this.scale2 = 0;
                this.scale3 = 0;
            }
            this.xcounter = xcounter;
            //this.height = height;
        }

        //Add selected GameObject to "prefabs" Array with "prefab" tag
        if (Selection.activeGameObject != null && !prefabs.Contains(Selection.activeGameObject) &&
            Selection.activeGameObject.tag == "LevelEditorAsset")
        {
            prefabs.Add(Selection.activeGameObject);
        }
    }
}